<template>
  <a-spin :spinning="confirmLoading">
    <a-form :form="model" class="form">
      <!-- <a-card class="card" title="标书费" :bordered="false"> -->
      标书费
      <a-form-item
        label="是否收取"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
        :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        :required="false"
      >
        <a-radio-group v-model="exChargeSettingInfo.bsfIsSq">
          <!-- <a-radio :value="1">收取</a-radio> -->
          <a-radio :value="2">不收取</a-radio>
        </a-radio-group>
      </a-form-item>
      <!-- </a-card> -->
      <!-- <a-card class="card" title="投标保证金" :bordered="false"> -->
      投标保证金
      <a-form-item
        label="是否收取"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
        :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        :required="false"
      >
        <a-radio-group v-model="exChargeSettingInfo.tbbzjIsSq">
          <a-radio :value="1">收取</a-radio>
          <a-radio :value="2">不收取</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="是否退还"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
        :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        :required="false"
      >
        <a-radio-group v-model="exChargeSettingInfo.tbbzjIsTh">
          <a-radio :value="1">退还</a-radio>
          <a-radio :value="2">不退还</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="收取方式"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
        :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        :required="false"
      >
        <a-radio-group v-model="exChargeSettingInfo.tbbzjSqfs">
          <a-radio :value="1">按标准</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="计算方式"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
        :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        :required="false"
      >
        <a-radio-group v-model="exChargeSettingInfo.tbbzjJsfs">
          <a-radio :value="1">定额</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="是否支持保函收取"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
        :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        :required="false"
      >
        <a-radio-group v-model="exChargeSettingInfo.tbbzjIsBhsq">
          <a-radio :value="1">支持</a-radio>
          <a-radio :value="2">不支持</a-radio>
        </a-radio-group>
      </a-form-item>
      <!-- </a-card> -->
      <a-table :columns="columns" :dataSource="exSectionList" :pagination="false">
        <template slot="computeMethod" slot-scope="text, record, index">
          {{ exChargeSettingInfo.tbbzjJsfs == '1' ? '定额' : '' }}
        </template>
        <template slot="amount" slot-scope="text, record, index">
          <a-input-number
            :key="col"
            style="margin: -5px 0"
            :value="text"
            placeholder="金额"
            @change="(e) => handleChange(e, record.id, 'amount')"
          />
        </template>
      </a-table>
    </a-form>
    <a-row style="margin-top: 10px;">
      <a-col :span="24">
        <ex-operation-record
          ref="exOperationRecord"
          :module="'exChargeSetting'"
          :entryId="exChargeSettingInfo.id"
        ></ex-operation-record>
      </a-col>
    </a-row>
    <div style="margin-top: 10px; display: flex; justify-content: end">
      <template
        v-if="exChargeSettingInfo.status == null || exChargeSettingInfo.status == 0 || exChargeSettingInfo.status == 3"
      >
<!--        <a-button @click="handleOk(0)" type="primary" icon="save">保存</a-button>-->
        <a-button @click="handleOk(1)" style="margin: 0 20px" type="primary" icon="check">提交</a-button>
      </template>
      <!-- <a-button @click="handleCancel">关闭</a-button> -->
    </div>
  </a-spin>
</template>

<script>
import { httpAction, getAction, postAction } from '@/api/manage'
import DetailList from '@/components/tools/DetailList'
const DetailListItem = DetailList.Item
import ExOperationRecord from '@/views/exOperationRecord/Index'

export default {
  components: {
    DetailList,
    DetailListItem,
    ExOperationRecord
  },
  name: 'Setting',
  props: {
    model: {},
    projectId: {},
  },
  data() {
    return {
      title: '操作',
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        exSection: {
          list: '/exProject/exProject/queryExSectionByMainId',
        },
        exChargeSetting: {
          queryByProjectId: '/exChargeSetting/exChargeSetting/queryByProjecrtId',
          save: '/exChargeSetting/exChargeSetting/save',
        },
      },
      columns: [
        {
          title: '标段名称',
          dataIndex: 'bidSectionName',
          key: 'bidSectionName',
          width: '20%',
          scopedSlots: { customRender: 'bidSectionName' },
        },
        {
          title: '计算方式',
          dataIndex: 'computeMethod',
          key: 'computeMethod',
          width: '20%',
          scopedSlots: { customRender: 'computeMethod' },
        },
        {
          title: '金额（元）',
          dataIndex: 'amount',
          key: 'amount',
          width: '20%',
          scopedSlots: { customRender: 'amount' },
        },
      ],
      exChargeSettingInfo: {
        id: '',
        projectId: '',
        projectCode: '',
        projectName: '',
        bsfIsSq: 0,
        tbbzjIsSq: 0, //投标保证金
        tbbzjIsTh: 0, //投标保证金是否退还
        tbbzjSqfs: 0, //投标保证金收取方式
        tbbzjJsfs: 0, //计算方式
        tbbzjIsBhsq: 0, //是否支持保函收取
        status: 0,
      },
      exSectionList: [],
    }
  },
  watch: {
    'model.projectId': {
      immediate: true,
      handler(val) {
        if (val != undefined && val != '') {
          this.loadList()
          this.loadExChargeSettingInfo()
        }
      },
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {
    // this.loadList()
    // this.loadExChargeSettingInfo()
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.loadList()
      this.loadExChargeSettingInfo()
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk(status) {
      const that = this
      var param = Object.assign({}, this.exChargeSettingInfo)
      param.status = status
      param.projectId = that.model.projectId
      param.projectCode = that.model.projectCode
      param.projectName = that.model.projectName
      param.exSectionList = that.exSectionList
      that.confirmLoading = true
      postAction(this.url.exChargeSetting.save, param).then((res) => {
        if (res.success) {
          that.exChargeSettingInfo.status = status
          that.$message.success(res.message)
          that.close()
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
          that.confirmLoading = false
        })
    },
    handleChange(value, key, column) {
      const newData = [...this.exSectionList]
      const target = newData.filter((item) => key === item.id)[0]
      if (target) {
        target[column] = value
        this.exSectionList = newData
      }
    },
    handleCancel() {
      this.close()
      this.$emit('close')
    },
    loadList() {
      var that = this
      let params = { id: that.model.projectId }
      getAction(this.url.exSection.list, params)
        .then((res) => {
          if (res.success) {
            this.exSectionList = res.result
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    loadExChargeSettingInfo() {
      var that = this
      let params = { projectId: that.model.projectId }
      getAction(this.url.exChargeSetting.queryByProjectId, params)
        .then((res) => {
          if (res.success) {
            if (res.result) {
              Object.assign(that.exChargeSettingInfo, res.result)
              var items = ['bsfIsSq', 'tbbzjIsSq', 'tbbzjIsTh', 'tbbzjSqfs', 'tbbzjJsfs', 'tbbzjIsBhsq']
              items.forEach((e) => {
                that.exChargeSettingInfo[e] = parseInt(that.exChargeSettingInfo[e])
              })
            }
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
  },
}
</script>

<style scoped>
</style>
